package com.renyi.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.renyi.http.HttpResult;
import com.renyi.http.ParamBean;
import com.renyi.model.MybatisService;
import com.renyi.service.TjxmService;
import com.renyi.util.JSONUtils;

/**
 * 
 * @author shs
 * 
 */
@Controller
public class TjxmController extends BaseController {

	@Autowired
	private MybatisService mybatisService;

	@Autowired
	private TjxmService tjxmService;

	private Logger log = LogManager.getLogger(this.getClass().getName());

	// 获取体检项目分类列表
	@RequestMapping("/getJcflList")
	public void getJcflList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getJcflListSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 获取体检项目列表
	@RequestMapping("/getJcxmlist")
	public void getJcxmlist(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);

			if (paramBean == null) {
				result.setCode(1003); //
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> map = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			// 分类编号
			String typeID = map.get("typeID") != null ? map.get("typeID").toString() : "";
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("typeID", typeID);
			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getJcxmlistSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1);
			result.setMsg(e.getMessage());
			;// //错误消息
			this.ajaxResultJson(response, result);
		}
	}

	// 查看已购买体检项目
	@RequestMapping("/getJcxmByBridList")
	public void getJcxmByBridList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			Integer page = (int) (paramMap.get("page") != null
					? Math.floor(Float.valueOf(paramMap.get("page").toString())) : 1);
			Integer pageSize = (int) (paramMap.get("pageSize") != null
					? Math.floor(Float.valueOf(paramMap.get("pageSize").toString())) : 10);

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			HashMap<String, Object> pMap = new HashMap<String, Object>();
			Integer beginRow = (page - 1) * pageSize + 1;
			pMap.put("beginRow", beginRow);
			pMap.put("endRow", pageSize * page);
			pMap.put("uid", uid);

			List<HashMap<String, Object>> list = this.mybatisService.findMybatisList("getJcxmByBridList", pMap);
			Long count = this.mybatisService.findIbatisListCount("getJcxmByBridCount", pMap);
			count = count != null ? count : 0;
			result.setCode(0); // 0：成功
			result.setData(list);
			result.setRecordCount(count.intValue());
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 检查项目明细
	@RequestMapping("/getJcxmmxByBrid")
	public void getJcxmmxByBrid(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String BLID = paramMap.get("BLID") != null ? paramMap.get("BLID").toString() : "";
			if ("".equals(uid) || "".equals(BLID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("BLID", BLID);
			List<HashMap<String, Object>> reportList = (List<HashMap<String, Object>>) this.mybatisService
					.findMybatisList("getJcxmmxByBrid", pMap);
			result.setCode(0); // 0：成功
			result.setData(reportList);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 查看体检结果文字和图片
	@RequestMapping("/getJcjgByBLH")
	public void getJcjgByBLH(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String BLH = paramMap.get("BLH") != null ? paramMap.get("BLH").toString() : "";
			if ("".equals(uid) || "".equals(BLH)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("BLH", BLH);
			List<HashMap<String, Object>> reportList = (List<HashMap<String, Object>>) this.mybatisService
					.findMybatisList("getReportInfoList", pMap);
			for (HashMap<String, Object> report : reportList) {
				List<HashMap<String, Object>> resultList = (List<HashMap<String, Object>>) this.mybatisService
						.findMybatisList("getResultInfoList", report);
				report.put("resultList", resultList);
			}

			result.setCode(0); // 0：成功
			result.setData(reportList);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 生成体检或化验订单
	@RequestMapping("/addJcxmOrder")
	public void addJcxmOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String OrderID = paramBean.getLimit().get("OrderID") != null
					? paramBean.getLimit().get("OrderID").toString() : "";
			if ("".equals(uid) || "".equals(OrderID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			paramMap.put("uid", uid);
			tjxmService.addBlxx(paramMap);

			result.setCode(0); // 0：成功
			// result.setData(reportList);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage()); // 错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 购买体检项目后生成门诊订单
	@RequestMapping("/createTJOrder")
	public void createTJOrder(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String tjIDList = paramMap.get("tjIDList") != null ? paramMap.get("tjIDList").toString() : "";
			if ("".equals(uid) || "".equals(tjIDList)) {
				result.setCode(-1); // 0：成功
				result.setMsg("有参数为空");
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("tjIDList", tjIDList);
			pMap.put("uid", uid);
			List<HashMap<String, Object>> listRes = this.mybatisService.procedureMybatisList("createTJOrderSQL", pMap);
			String resType = "";
			if (listRes.size() > 0) {
				resType = listRes.get(0).get("resType") != null ? listRes.get(0).get("resType").toString() : "0";
				if (resType.equals("0")) {
					result.setCode(0); // 0：成功
				} else {
					result.setCode(-1); // 0：失败
				}

			}

			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 查找体检列表
	@RequestMapping("queryTJList")
	public void queryTJList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String keyword = paramMap.get("keyword") != null ? paramMap.get("keyword").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用戶ID参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("keyword", keyword);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("queryTJListSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}
}
